package com.jcl.core.utils;

import com.jcl.core.constants.Constant;
import org.apache.commons.beanutils.BeanMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class BaseUtils {

    private static String random_str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    public static String randomString(int length){
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for(int i = 0 ; i < length; i ++){
           int number = random.nextInt(random_str.length());
           buffer.append(random_str.charAt(number));
        }
        return buffer.toString();
    }

    public static String madeCorpCode(String prefix, String temp){
        if(StringUtils.isEmpty(temp)){
            return null;
        }
        temp = temp.trim();
        if(null == prefix){
            prefix = Constant.CORP_CODE_PREFIX;
        }
        StringBuffer code = new StringBuffer(prefix);

        int num = 7 - temp.length();
        for(int i = 0; i < num; i ++){
            code.append("0");
        }
        code.append(temp);
        return code.toString();
    }

    public static String madeCorpCode(String prefix, Integer temp){

        return madeCorpCode(prefix, temp.toString());
    }

    public static <T> Map<String, Object> convertToMap(T bean){
        Map<String, Object> map = new HashMap<>();
        if(bean != null){
            BeanMap beanMap = new BeanMap(bean);
            for (Map.Entry<Object, Object> entry: beanMap.entrySet()){
                map.put(String.valueOf(entry.getKey()), entry.getValue());
            }
        }
        return map;
    }
}
